// api/topic.js
import { request } from "@/utils/request";




// 获取经书列表的参数接口
interface GetTopicListParams {
    keyword?: string;
    pageNum: number;
    pageSize: number;
}


interface GetCommentsListParams {
    topicId?: string;
    pageNum: number;
    pageSize: number;
}


interface JingshuUser {
    id: number;
    username: string;
    password: string;
    phoneNumber: string;
    isActive: number;
    isAdmin: number;
    level: number;
    birthDate: null,
    address: null,
    gender: number;
    avatarUrl: null,
    dharmaName: string;
    totalMerit: number;
    createTime: string;
    updateTime: string;
}

interface Comment {
    id: number;
    topicId: number;
    userId: number;
    content: string;
    side: number;
    status: number;
    createTime: string;
    updateTime: string;
}


  export interface Topic {
    id: number;
    userId: number;
    title: string;
    content: string;
    status: number;
    prosTitle: string;
    consTitle: string;
    coverUrl: string;
    positive: string;
    negative: string;
    createTime: string;
    updateTime: string;
    user: JingshuUser;
    comments: Array<Comment>;
} 



export interface ResponseData<T> {
    code: number;
    row?: T;
    msg: string;
}

export const getTopicList = async (params: GetTopicListParams) => {
  try {
    const { keyword = '', pageNum, pageSize } = params;
    const response = await request.get('/scripture/topic/list', {
      data: {
        keyword,
        pageNum,
        pageSize
      }
    });
    return response as any;
  } catch (error) {
    console.error('获取主题列表失败:', error);
    return {
      code: -1,
      msg: '网络请求失败',
      rows: [],
      total: 0
    };
  }
};


export const getTopicDetail = async (detailId: number) => {
  try {
    const response = await request.get('/scripture/topic', {
      data: { detailId }
    });
    
    if (response.code === 200 && response.data) {
      return response.data as any;
    }
    
    return null;
  } catch (error) {
    console.error('获取主题细节失败:', error);
    throw error;
  }
};




export function createTopic(data:any) {
  return request.post('/api/topic/create', data)
}

export async function addComment(data:any) {
  return await request.post('/scripture/comment/add', data)
}

export async function addReply(data:any) {
  return await request.post('/scripture/reply/add', data)
}

export async function handleDeleteReply(data:any) {
  let arrays = [data]
  return await request.post('/scripture/reply/delete', arrays)
}


export const getCommentList = async (params: GetCommentsListParams) => {
  try {
    const { topicId , pageNum, pageSize } = params;
    const response = await request.get('/scripture/comment/list', {
      data: {
        topicId,
        pageNum,
        pageSize
      }
    });
    return response as any;
  } catch (error) {
    console.error('获取主题列表失败:', error);
    return {
      code: -1,
      msg: '网络请求失败',
      rows: [],
      total: 0
    };
  }
}


export async function handleDeleteComment(data:any) {
  let arrays = [data]
  return await request.post('/scripture/comment/delete', arrays)
}




// 点赞
export async function handleTopicLike(data:any) {
  return await request.post('/scripture/topic/like', data)
}


// 获取点赞数
export async function handleLikedUsers(detailId:number) {
  return await request.get('/scripture/topic/getLikedUsers',  {
    data: { id: detailId }
  })
}


// 收藏
export async function handleTopicCollect(data:any) {
  return await request.post('/scripture/topic/collect', data)
}


// 获取收藏数
export async function handleCollectedUsers(detailId:number) {
  return await request.get('/scripture/topic/getCollectedUsers',  {
    data: { id: detailId }
  })
}



// 是否支持正反
export async function handleSide(data:any) {
  return await request.post('/scripture/topic/side', data)
}



// 获取正方反方数
export async function handleSides(detailId: number) {
  console.log('test', detailId)
  return await request.get('/scripture/topic/getSides',  {
    data: { id: detailId }
  })
}

